package com.resou.reader.discovery.comment;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BookLabelBean;
import com.resou.reader.api.entry.BookListCommentBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CommentPresenter extends ResouBasePresenter<ICommentContract.ICommentView> implements ICommentContract.ICommentPresenter {

    StoreService storeService;

    public CommentPresenter(ICommentContract.ICommentView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    /**
     * 查询评论
     *
     * @param bookListId
     * @param page
     */
    @Override
    public void quearCommentData(String bookListId, int page) {
        mView.showProgress();
        storeService.commentBook(bookListId, page, pageSize, UserUtil.getToken())
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<BookListCommentBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<BookListCommentBean> bookListCommentBeanResult) {
                        int totalNum = bookListCommentBeanResult.getData().getTotalPage() * (pageSize);
                        int localtotalNum = (page + 1) * pageSize;
                        if (localtotalNum < totalNum) {
                            mView.showCommentData(bookListCommentBeanResult.getData().getComments(), totalNum);
                            mView.getSmartRefreshLayout().finishLoadMore(true);
                        } else {
                            if (totalNum == 0) {
                                mView.showEmpty();
                            }
                            mView.showCommentData(bookListCommentBeanResult.getData().getComments(), totalNum);
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showEmpty();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void praiseComment(String userId) {

    }

    @Override
    public void unPraiseComment(String userId) {

    }


}
